#!/usr/bin/env python3

from django.conf.urls import url

#超级管理员=权限等级1
#管理员=权限等级2
#技术=权限等级3
#策划=权限等级4
#运营=权限等级5
#客服=权限等级6
#外包客服=权限等级9
#最低权限=权限等级10
"""
GROUP_LEVEL_1		100,200,300,400
GROUP_LEVEL_2		100,201,301,400
GROUP_LEVEL_6		100,303,400
...
...

"""
allGroupPermission = {
    31:{
            'name':'超级管理员=权限等级1',
            'auth':'100, 200, 300, 400',
        },
    32:{
        'name':'管理员=权限等级2',
        'auth':'100, 201, 301, 400',
    },
    36:{
        'name':'客服=权限等级6',
        'auth':'100,303,400',
    },
    39:{
        'name':'自订权限',
        'auth':'',
    },
}


#定义菜单
actions = {}
permissionLink = {}

trunkMenu = {
            'user':{'permission':100,'lang':'客服'},
            'op':{'permission':200,'lang':'运营'},
            'pay':{'permission':300,'lang':'支付'},
            'stat':{'permission':400,'lang':'统计'},
    }

branchMenu = {
    'user':{
              'userinfo':{'permission':101,'lang':'用户信息'},
              'building':{'permission':102,'lang':'玩家订单'},
#              'army':{'permission':103,'lang':'menu_user_army'},
#              'goods':{'permission':104,'lang':'menu_user_goods'},

    },
    'op':{
            'push':{'permission':201,'lang':'邮件'},
#            'announce_mail':{'permission':202,'lang':'menu_op_announce_mail'},
#            'usermail':{'permission':203,'lang':'menu_op_usermail'},
#            'refund':{'permission':204,'lang':'menu_op_refund'},
    },
    'pay':{
            'detail':{'permission':301,'lang':'支付明细'},
#            'analyze':{'permission':302,'lang':'menu_pay_pay_analyze'},
#            'roi':{'permission':303,'lang':'menu_pay_roi'},
},
    'stat':{
            'dau':{'permission':401,'lang':'日活跃'},
#            'registerdata':{'permission':402,'lang':'menu_stat_register'},
#            'regremain':{'permission':403,'lang':'menu_stat_regremain'},

    },
}



def initMenu(groupid, permission = False):
    """
             * see 生成导航菜单
             *
             * param  groupid
             *
             * param array permission
             *
             * return array
             *
    """
    groupid = int(groupid)
    if permission:
        li = permission.split(',')
        ALLauth = allGroupPermission[groupid]['auth'].split(',') + li
    elif int(groupid) in allGroupPermission:
        ALLauth = allGroupPermission[groupid]['auth'].split(',')
    else:
        return

    permission = []
    for i in ALLauth:
        permission.append(int(i))

    permission = list(set(permission))
    permission.sort(reverse=True)
    print(permission)
    supers = True if int(groupid/10) == 1 else False
    hidePermission = True if int(groupid/10) == 2 else False

    if not supers and  not permission:
        return

    menu = {}
    for tab, value in trunkMenu.items():
        if supers or value['permission'] in permission:
            trunkPermission = True
            branchPermission = True
        else:
            branchPermission = False
            trunkPermission = False
            for subTab, subValue in branchMenu[tab].items():
                    if permission[subValue['permission']]:
                            trunkPermission = True
        if trunkPermission :
            menu[tab] = {"name" :value['lang'],"permit" : value['permission'],'sub_menu':[]}

            for subTab, subValue in branchMenu[tab].items():
                if 'hide' in subValue.keys() and not hidePermission and not supers:
                        continue
                if branchPermission or permission[subValue['permission']]:
                        tmp = {
                          "name" : subValue['lang'],
                          "permit" : subValue['permission'],
                          "href" : subTab,
                          "action": subTab,
                          }
                        menu[tab]['sub_menu'].append(tmp)

    return menu


if __name__ == "__main__":
    menus = initMenu(31, [100,200,300,400])
    print(menus['sub_menu'])
